﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Threading.Tasks;

namespace TK.Common.AspNetCore
{
    /// <summary>
    /// 控制器基类
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    [Authorize]
    public class ApiController : ControllerBase
    {
        /// <summary>
        /// 获取请求Body
        /// </summary>
        protected async Task<string> GetRequestBodyAsync()
        {
            var requestTxt = string.Empty;

            Request.EnableBuffering();
            Request.Body.Seek(0, SeekOrigin.Begin);

            using (var reader = new StreamReader(Request.Body, leaveOpen: true))
            {
                requestTxt = await reader.ReadToEndAsync();
            }
            Request.Body.Seek(0, SeekOrigin.Begin);
            return requestTxt;
        }
    }
}
